package com.chinasoft.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.chinasoft.entity.Book;
import com.chinasoft.entity.Page;
import com.chinasoft.service.BookService;

@Controller
@RequestMapping(method = { RequestMethod.POST, RequestMethod.GET })
public class BookContrller {
	@Autowired
	private BookService bs;
	
	/*
	 * @RequestMapping("/insert") public String insert() { return
	 * "/BookManager/addBook.jsp"; }
	 */
	//Modify By Cara
	@RequestMapping("/bookList")
	public String selectAllBook(Model m, String currentpage) {
		List<Book> list = bs.findAllBooks();
		int id = currentpage == null ? 1 : Integer.parseInt(currentpage);
		Page p = new Page(list.size(), id);
		List<Book> booklist = bs.selectAllBook(p);
		m.addAttribute("booklist", booklist);
		m.addAttribute("p", p);
		return "/BookManager/booklist.jsp";
	}

	@RequestMapping("showBook")
	public String selectBookById(Book book, Model m) {
		Book bo = bs.selectBookById(book);
		m.addAttribute("book", bo);
		return "/BookManager/showBook.jsp";
	}

	@RequestMapping("/updateBook")
	public String updateBook(Book book) {
		bs.updateBook(book);
		return "redirect:/bookList.action";

	}

	@RequestMapping("/selectByNameAndType")
	public String selectByNameAndType(Book book) {
		bs.selectByNameAndType(book);
		return "/BookManager/booklist.jsp";

	}
	@RequestMapping("/addBook")
	public String addBook(Book book) {
		bs.addBook(book);
		return "redirect:/bookList.action";
	}
}
